<?php

namespace app\admin\model;

use app\common\model\TimeModel;

class Town extends TimeModel
{
    protected $name = "town";
    protected $deleteTime = false;

    public function community()
    {
        return $this->hasMany(Community::class);
    }

    public static function getTownList()
    {
        $list = Town::field('name as label,id as value')->select();
        foreach ($list as $k => &$v) {
            $v['extra'] = $k;
        }
        return $list;
    }

    public static function getTownList2()
    {
        $none = [0 => '无'];
        $list = Town::column('name', 'id');
        $list = array_merge($none, $list);
        return $list;
    }

    /**
     * 乡镇名查ID
     * @param string $name 乡镇名
     * @return int|mixed
     */
    public static function getNameById(string $name)
    {
        return self::where('name', $name)->value('id') ?? 0;
    }
}